﻿using System.Collections.Generic;
using System.Windows.Input;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Facade;
using StockholmsStad.Windows.UI.Infrastructure.Interfaces;
using StockholmsStad.Windows.UI.Modules.Couple.BusinessEntities;
using StockholmsStad.Windows.UI.Modules.Couple.Interfaces;
using StockholmsStad.Windows.UI.Modules.Couple.Models;
using StockholmsStad.Windows.UI.Modules.Couple.Events;

namespace StockholmsStad.Windows.UI.Modules.Couple.Services
{
    public class CeremonyService : ICeremonyService
    {
        private IApplicationService applicationService;
        private List<CeremonySpecialNeed> ceremonySpecialNeeds;
        private List<ValueIdPair> ceremonyLanguages;
        private List<ValueIdPair> certificateLanguages;
        private List<ValueIdPair> ceremonyTypes;

        private readonly IEventAggregator eventAggregator;

        [Dependency]
        public IApplicationService ApplicationService
        {
            set { applicationService = value; }
        }

        public CeremonyService(IEventAggregator aggregator)
        {
            eventAggregator = aggregator;
        }

        #region ICeremonyService Members

        public CeremonyPresentationModel InitializeNewCeremony()
        {
            saved = false;
            var model = new CeremonyPresentationModel();            
            var user = applicationService.GetCurrentUser();
            var ceremonyFacade = new CeremonyFacade(user);
            var defaultLang = ceremonyFacade.GetDefaultLanguage();
            model.CeremonyLanguage = defaultLang;
            model.CertificateLanguage = defaultLang;
            model.Saved += CeremonySaved;
            model.Changed += CeremonyChanged;
            model.IsInChangeMode = false;
            return model;            
        }

        public CeremonyPresentationModel GetCeremony(int ticketId)
        {
            saved = false;
            var model = new CeremonyPresentationModel();

            var user = applicationService.GetCurrentUser();
            var facade = new TicketFacade(user);
            var ceremonyBooking = facade.GetCeremony(ticketId);
            CreateCeremonyModel(model, ceremonyBooking);
            model.TicketId = ticketId;

            model.Saved += CeremonySaved;
            model.Changed += CeremonyChanged;
            model.IsInChangeMode = false;
            return model;
        }

        public List<ValueIdPair> GetCeremonyTypes()
        {
            var user = applicationService.GetCurrentUser();
            var facade = new CeremonyFacade(user);
            ceremonyTypes = facade.GetCeremonyTypes();
            return ceremonyTypes;
        }

        public List<ValueIdPair> GetCeremonyLanguages()
        {
            var user = applicationService.GetCurrentUser();
            var facade = new CeremonyFacade(user);
            ceremonyLanguages = facade.GetCeremonyLanguages();
            return ceremonyLanguages;
        }

        public List<ValueIdPair> GetCertificateLanguages()
        {
            var user = applicationService.GetCurrentUser();
            var facade = new CeremonyFacade(user);
            certificateLanguages = facade.GetCertificateLanguages();
            return certificateLanguages;
        }

        public ValueIdPair GetDefaultLanguage()
        {
            var user = applicationService.GetCurrentUser();
            var facade = new CeremonyFacade(user);
            return facade.GetDefaultLanguage();
        }

        public List<CeremonySpecialNeed> GetSpecialNeeds()
        {
            var user = applicationService.GetCurrentUser();
            var facade = new CeremonyFacade(user);
            var list = facade.GetSpecialNeeds();

            ceremonySpecialNeeds = new List<CeremonySpecialNeed>();
            foreach(var item in list)
            {
                ceremonySpecialNeeds.Add(new CeremonySpecialNeed
                                             {
                                                 Id = item.Id,
                                                 IsSelected = false,
                                                 Name = item.Name
                                             });
            }

            return ceremonySpecialNeeds;
        }


        #endregion

        #region Ceremony events

        // ful fix, av någon anledning kommer man in i denna kod 2 ggr, troligen updatesourcetrigger 
        // som inte funkar bra med presentationsmodellen. 
        private bool saved;
        private void CeremonySaved(object sender, DataEventArgs<CeremonyPresentationModel> e)
        {
            if (e != null && e.Value != null && !saved)
            {
                Mouse.OverrideCursor = null;
                CeremonyPresentationModel ceremonyModel = e.Value;

                var ceremony = CreateCeremonyModel(ceremonyModel);

                var user = applicationService.GetCurrentUser();
                var facade = new TicketFacade(user);
                facade.UpdateCeremony(ceremonyModel.TicketId.Value, ceremony);
                saved = true;

                eventAggregator.GetEvent<CeremonyChangedEvent>().Publish(ceremonyModel);
            }
        }

        private void CeremonyChanged(object sender, DataEventArgs<bool> e)
        {
            //Det här måste testas mera då det inte fungerar
            //eventAggregator.GetEvent<IsInChangeModeEvent>().Publish(e.Value);
        }


        #endregion

        #region converter methods

        private static Ceremony CreateCeremonyModel(CeremonyPresentationModel ceremonyModel)
        {
            var ceremony = new Ceremony
                               {
                                   ImpedimentDocLatestDate = ceremonyModel.HPLatestDate,
                                   ImpedimentDocIssuedDate = ceremonyModel.HPIssuedDate,
                                   CeremonyLanguage = ceremonyModel.CeremonyLanguage,
                                   CertificateLanguage = ceremonyModel.CertificateLanguage,
                                   Interpreter = ceremonyModel.Interpreter,
                                   Note = ceremonyModel.Note,
                                   OtherLanguage = ceremonyModel.OtherLanguage
                               };

            if(ceremonyModel.CeremonySpecialNeeds != null)
            {
                ceremony.SpecialNeeds = new List<ValueIdPair>();
                foreach (var item in ceremonyModel.CeremonySpecialNeeds)
                {
                    ceremony.SpecialNeeds.Add(new ValueIdPair
                                                  {
                                                      Name = item.Name,
                                                      Id = item.Id,
                                                  });
                }
            }

            return ceremony;
        }

        public void CreateCeremonyModel(CeremonyPresentationModel model, CeremonyBooking ceremonyBooking)
        {
            model.Note = string.IsNullOrEmpty(ceremonyBooking.Ceremony.Note)
                             ? string.Empty
                             : ceremonyBooking.Ceremony.Note;

            model.OtherLanguage = string.IsNullOrEmpty(ceremonyBooking.Ceremony.OtherLanguage)
                                      ? string.Empty
                                      : ceremonyBooking.Ceremony.OtherLanguage;

            model.Interpreter = ceremonyBooking.Ceremony.Interpreter;

            if(ceremonySpecialNeeds != null && ceremonyBooking.Ceremony.SpecialNeeds != null)
            {
                model.CeremonySpecialNeeds = new List<CeremonySpecialNeed>();
                foreach (var item in ceremonyBooking.Ceremony.SpecialNeeds)
                {
                    model.CeremonySpecialNeeds.Add(
                        new CeremonySpecialNeed {Id = item.Id, Name = item.Name, IsSelected = true}
                        );
                }
            }

            model.CeremonyLanguage = ceremonyBooking.Ceremony.CeremonyLanguage;
            model.CertificateLanguage = ceremonyBooking.Ceremony.CertificateLanguage;

            if (ceremonyBooking.Ceremony.ImpedimentDocIssuedDate.HasValue)
                model.HPHasArrived = ceremonyBooking.Ceremony.ImpedimentDocIssuedDate.HasValue
                        ? true
                        : false;

            model.MinisterFirstName = ceremonyBooking.Minister != null ? ceremonyBooking.Minister.FirstNames : string.Empty;
            model.MinisterLastName = ceremonyBooking.Minister != null ? ceremonyBooking.Minister.LastName : string.Empty;

            model.HPIssuedDate = ceremonyBooking.Ceremony.ImpedimentDocIssuedDate;
            model.HPEarliestDate = ceremonyBooking.Ceremony.ImpedimentDocEarliestDate;
            model.HPLatestDate = ceremonyBooking.Ceremony.ImpedimentDocLatestDate;

            model.Saved += CeremonySaved;
            model.Changed += CeremonyChanged;
        }

        #endregion
    }
}
